﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebLayer;

namespace WebParser
{
    public class WebPageCache
    {
        Dictionary<Type, SiteWebPage> WebPages = new Dictionary<Type,SiteWebPage>();
        LoginToken m_loginToken = null;
        public LoginToken LoginToken
        {
            set
            {
                foreach(SiteWebPage page in WebPages.Values)
                {
                    page.LoginToken = value;
                }
                m_loginToken = value;
            }
            get
            {
                return m_loginToken;
            }
        }

        public PageType GetPage<PageType>() where PageType : SiteWebPage, new()
        {
            if(WebPages.Keys.Contains(typeof(PageType)) )
            {
                return (PageType)WebPages[typeof(PageType)];
            }
            
            PageType newPage = new PageType();
            newPage.LoginToken = LoginToken;
            newPage.Request();

            WebPages[typeof(PageType)] = newPage;
            return newPage;
        }

        public void DiscardPage<PageType>() where PageType : SiteWebPage
        {
            if (WebPages.Keys.Contains(typeof(PageType)) == true)
            {
                WebPages.Remove(typeof(PageType));
            }
        }

        public void Discard()
        {
            WebPages = new Dictionary<Type, SiteWebPage>();
        }        
    }
}
